package com.example.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import sun.rmi.runtime.Log;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: H_tao_X
 * Date: 2022-08-01
 * Time: 19:08
 */

/**
 * 步骤：
 * 1. 添加 Configuration 注解
 * 2. 实现 WebMvcConfigurer 接口
 * 3. 重写 addInterceptors 方法
 */
@Configuration // 一定是 Configuration
public class AppConfig implements WebMvcConfigurer {
    // 写法二: 属性注入
    @Autowired
    private LoginIntercept loginIntercept;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 这里放 拦截器
// 这种写法就不需要属性注入
// 写法一: registry.addInterceptor(new LoginIntercept());
        // 写法二: 属性注入
        registry.addInterceptor(loginIntercept).
                addPathPatterns("/**"). // 拦截所有的 url
                excludePathPatterns("/user/login"). // 不拦截登录接口
                excludePathPatterns("/user/reg").
                excludePathPatterns("/login.html").
                excludePathPatterns("/reg.html").
                excludePathPatterns("/**/*.js").
                excludePathPatterns("/**/*.css").
                excludePathPatterns("/**/*.png").
                excludePathPatterns("/user/index");
    }

    // 统一访问的前缀添加 8080/api/user/login ---
//    @Override
//    public void configurePathMatch(PathMatchConfigurer configurer) {
//        configurer.addPathPrefix("api",c -> true);
//    }
}
